package com.hj.system.base.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class ServerMessage {
    private Socket socket;

    public ServerMessage(Socket socket) {
        this.socket = socket;
    }

    public void sendMessage(String message) {
        OutputStream out = null;
        try {
            if (socket != null) {
                out = socket.getOutputStream();
                for (int i = 0; i < 20; i++) {
                    Thread.sleep(1000);
                    out.write(("server saying: " + message).getBytes());
                    out.flush();//清空缓存区的内容
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

//    @Override
//    public void run() {
//        super.run();
//        OutputStream out = null;
//        try {
//            if (socket != null) {
//                out = socket.getOutputStream();
//                for (int i = 0; i < 3; i++) {
//                    Thread.sleep(1000);
//                    out.write(("server saying: ").getBytes());
//                    out.flush();//清空缓存区的内容
//                }
////                do {
////
////                } while (true);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//    }
}
